package com.baizhi.service.impl;

import com.baizhi.dao.AdminDAO;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDAO adminDAO;
    @Override
    @Transactional(readOnly = true)
    public Admin selectOne(String name, String password, String code, HttpSession session) {
        Admin admin = adminDAO.selectOne(name);
        if (admin==null || !admin.getPassword().equals(password)){
            admin = null;
            throw new RuntimeException("用户不存在或密码错误");
        }
        return admin;
    }

    @Override
    public PageInfo<Admin> selectAll(Integer page,Integer limit) {
        PageHelper.startPage(page,limit);
        PageInfo info = new PageInfo(adminDAO.selectAll());
        return info;
    }

    @Override
    @Transactional
    public void insert(Admin a) {
        adminDAO.insert(a);
    }
}
